<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Number Generator</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        .button {
            border-radius: 10px;
            border: 1px solid #ccc;
            margin: 2px 5px;
            padding: 5px 8px;
            cursor: pointer;
            background-color: #f0f0f0;
        }
        .button:hover {
            background-color: #e0e0e0;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin-top: 10px;
            padding: 10px;
            box-sizing: border-box;
        }
        .output-section {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>随机产生20个4位整数并从小到大进行排序</h2>

        <div class="button-container">
            <button id="generateBtn" class="button">随机产生20个整数</button>
            <button id="filterBtn" class="button">找出能被5整除的整数</button>
            <button id="resetBtn" class="button">重置</button>
        </div>

        <div class="output-section">
            <textarea id="outputArea" readonly></textarea>
        </div>
    </div>

    <script>
        let numbers = [];
        function generateRandomNumbers() {
            numbers = [];
            for (let i = 0; i < 20; i++) {
                numbers.push(Math.floor(Math.random() * 9000) + 1000);
            }
            numbers.sort((a, b) => a - b);

            // Display the results
            $('#outputArea').val(
                '随机产生20个4位整数，分别如下：\n' +
                numbers.join(', ') +
                '\n从小到大排序后结果为：\n' +
                numbers.join(', ')
            );
        }
        function filterDivisibleBy5() {
            if (numbers.length === 0) {
                alert('请先生成随机数！');
                return;
            }
            const divisibleBy5 = numbers.filter(num => num % 5 === 0);

            let currentText = $('#outputArea').val();
            $('#outputArea').val(
                currentText +
                '\n能被5整除的整数有：\n' +
                (divisibleBy5.length > 0 ? divisibleBy5.join(', ') : '无')
            );
        }
        // Reset the output
        function resetOutput() {
            numbers = [];
            $('#outputArea').val('');
        }
        $(document).ready(function() {
            $('#generateBtn').click(generateRandomNumbers);
            $('#filterBtn').click(filterDivisibleBy5);
            $('#resetBtn').click(resetOutput);
        });
    </script>
</body>
</html>